package cn.zyx.sys.entity;

import cn.zyx.common.annotation.IgnoreSwaggerParameter;
import cn.zyx.common.entity.DataEntity;
import com.alibaba.fastjson2.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.collect.Lists;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 用户Entity
 * @author zyx
 */
@Component
@EqualsAndHashCode(callSuper = true)
@Data
public class User extends DataEntity<User> {

	private static final long serialVersionUID = 1L;

	// 归属公司
	@IgnoreSwaggerParameter
	private Office company;
	// 归属部门
	@IgnoreSwaggerParameter
	private Office office;
	// 登录名
	private String loginName;
	// 密码
	private String password;
	// 工号
	private String no;
	// 姓名
	private String name;
	// 邮箱
	private String email;
	// 电话
	private String phone;
	// 手机
	private String mobile;
	@IgnoreSwaggerParameter
	// 最后登陆IP
	private String loginIp;
	@IgnoreSwaggerParameter
	// 最后登陆日期
	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	private Date loginDate;
	// 是否允许登陆
	private String loginFlag;
	// 头像
	private String photo;
	// 二维码
	private String qrCode;
	// 原登录名
	private String oldLoginName;
	// 新密码
	private String newPassword;
	// 签名
	private String sign;
	@IgnoreSwaggerParameter
	// 上次登陆IP
	private String oldLoginIp;
	// 上次登陆日期
	@IgnoreSwaggerParameter
	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	private Date oldLoginDate;
	// 根据角色查询用户条件
	@IgnoreSwaggerParameter
	private Role role;
	// 用户岗位
	private Post post;
	// 角色列表信息
    @IgnoreSwaggerParameter
	@JsonIgnore
	private List<Role> roleList = Lists.newArrayList(); // 拥有角色列表


	// ---------------------------  构造方法

	public User() {
		super();
	}
	public User(String id) {
		super(id);
	}
	public User(String id, String loginName) {
		super(id);
		this.loginName = loginName;
	}
	public User(Role role) {
		super();
		this.role = role;
	}


	// ---------------------------  get set 方法

	public String getId() {
		return id;
	}

	//@ExcelField(title="备注", align=1, sort=900)
	public String getRemarks() {
		return remarks;
	}

	//@ExcelField(title="创建时间", type=0, align=1, sort=90)
	public Date getCreateDate() {
		return createDate;
	}

	public String getOldLoginIp() {
		if (oldLoginIp == null){
			return loginIp;
		}
		return oldLoginIp;
	}

	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	public Date getOldLoginDate() {
		if (oldLoginDate == null){
			return loginDate;
		}
		return oldLoginDate;
	}

    public List<String> getRoleIdList() {
		List<String> roleIdList = Lists.newArrayList();
		for (Role role : roleList) {
			roleIdList.add(role.getId());
		}
		return roleIdList;
	}

	public void setRoleIdList(List<String> roleIdList) {
		roleList = Lists.newArrayList();
		for (String roleId : roleIdList) {
			Role role = new Role();
			role.setId(roleId);
			roleList.add(role);
		}
	}

	/**
	 * 用户拥有的角色名称字符串, 多个角色名称用','分隔.
	 */
	/*public String getRoleNames() {
		return Collections3.extractToString(roleList, "name", ",");
	}

	public String getRoleIds() {
		return Collections3.extractToString(roleList, "id", ",");
	}*/


	/**
	 * 判断用户是否是admin用户
	 * @return
	 */
	public boolean isAdmin(){
		return isAdmin(this.id);
	}

	public static boolean isAdmin(String id){
		return StringUtils.isNotEmpty(id) && "1".equals(id);
	}


	/**
	 * 判断用户是否是超级用户
	 * @param superUser
	 * @return
	 */
	public boolean isSuperUser(String superUser){
		String[] users = superUser.split(",");
		return isSuperUser(this.loginName,users);
	}

	public static boolean isSuperUser(String account,String[] users){
		List<String> superUser = new ArrayList<>();
        superUser.addAll(Arrays.asList(users));
		if (!superUser.isEmpty()) {
			if (StringUtils.isNotEmpty(account) && superUser.contains(account)) {
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
	}

	@Override
	public String toString() {
		return id;
	}

}
